﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Member.BLL
{
    public static class Md5Encrypt
    {
        /// <summary>
        /// 提供字符串根据指定的编码获取MD5值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="encoding">编码格式</param>
        /// <returns>MD5值</returns>
        public static string GetMd5ByString(string str, Encoding encoding)
        {
            MD5 md5 = MD5.Create();
            byte[] bs = encoding.GetBytes(str);
            bs = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (var item in bs)
            {
                sb.Append(item.ToString("x"));
            }
            return sb.ToString();
        }
        /// <summary>
        /// 提供字符串获取MD5值(UTF-8编码)
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>MD5值</returns>
        public static string GetMd5ByString(string str)
        {
            return GetMd5ByString(str, Encoding.UTF8);
        }
    }
}
